package bank.com;

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import bank.dao.Account;
import bank.db.DBoper;


public class Bank extends JFrame implements ActionListener{
	private JButton jb1,jb2;
	private JLabel  lab1,lab2;
	private JTextField jtf1;
	private JPasswordField jpwd1;
	private JPanel jp1,jp2,jp3;
	JFrame jf =new JFrame("ATM");
	Account user=new Account();
	public Bank(){
		
	Container container=jf.getContentPane();
	
	jb1=new JButton("登录");
	jb2=new JButton("开户");
	lab1=new JLabel("卡号: ");
	lab2=new JLabel("密码: ");
	jtf1=new JTextField(12);
	jpwd1=new JPasswordField(12);
	jp1=new JPanel();
	jp2=new JPanel();
	jp3=new JPanel();
	
	jp1.setBounds(40, 80,200, 50);
	jp2.setBounds(40, 150,200, 50);
	jp3.setBounds(40, 260, 200, 50);
	
	jp1.add(lab1);
	jp1.add(jtf1);
	jp2.add(lab2);
	jp2.add(jpwd1);
	jp3.add(jb1);
	jp3.add(jb2);
	
	container.setLayout(null);
	container.add(jp1);
	container.add(jp2);
	container.add(jp3);
	
	jf.setVisible(true);
	jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
	jf.setSize(300, 400);
	jf.setLocation(100, 100);
	
	
	jb1.addActionListener(this);
	jb2.addActionListener(this);
	
     
	}
	
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		DBoper db=new DBoper();
		db.getConnection();
		
		if (e.getSource()==jb1){
		
			 if(jtf1.getText().equals("")||jpwd1.getText().equals(""))
				  javax.swing.JOptionPane.showMessageDialog(this,"用户名和密码不能为空"); 
			 else{
			try {
				
				int i=Integer.parseInt(jtf1.getText()) ;
				String str=jpwd1.getText();
				user.setCard_id(i);
				user.setPassword(str);
				}catch(Exception ec){
					javax.swing.JOptionPane.showMessageDialog(this,"用户名和密码不能为空"); 	
					
				}
		  if(jtf1.getText().equals("")||jpwd1.getText().equals(""))
			  javax.swing.JOptionPane.showMessageDialog(this,"用户名和密码不能为空"); 
		    if(db.pstmtQuery(user)){
		    	Operate op=new Operate();
		    	op.user.setCard_id(user.getCard_id());
		    	jf.dispose();
		    	}
		    else if(db.pstmtQuery_cardid(user))
		    	javax.swing.JOptionPane.showMessageDialog(this,"密码不正确");
		    else 
		    	javax.swing.JOptionPane.showMessageDialog(this,"无此用户");
			 }
		  }
		if(e.getSource()==jb2){
			jf.dispose();
			new Register();
		}
		    	
				 
			 
		
	}
	
}

 class Operate extends JFrame implements ActionListener{
	private JButton bt1,bt2,bt3,bt4,bt5,bt6,bt7,bt8;
	private JPanel jp1,jp2,jp3,jp4;
	JFrame jf =new JFrame("ATM");
	Account user=new Account();
	
	   Operate(){
		   
		Container container=jf.getContentPane();
		
		bt1 = new JButton("查询");
		bt2 = new JButton("存钱");
		bt3 = new JButton("取钱");
		bt4 = new JButton("转账");
		bt5 = new JButton("改密");
		bt6 = new JButton("注销");
		bt7 = new JButton("退卡");
		bt8 = new JButton("返回");
		jp1=new JPanel();
		jp2=new JPanel();
		jp3=new JPanel();
		jp4=new JPanel();
		

		jp1.setBounds(40, 60,200, 50);
		jp1.setLayout(new FlowLayout(FlowLayout.CENTER,40,10));
		jp2.setBounds(40, 130,200, 50);
		jp2.setLayout(new FlowLayout(FlowLayout.CENTER,40,10));
		jp3.setBounds(40, 200, 200, 50);
		jp3.setLayout(new FlowLayout(FlowLayout.CENTER,40,10));
		jp4.setBounds(40, 270, 200, 50);
		jp4.setLayout(new FlowLayout(FlowLayout.CENTER,40,10));
		
		
		jp1.add(bt1);
		jp1.add(bt2);
		jp2.add(bt3);
		jp2.add(bt4);
		jp3.add(bt5);
		jp3.add(bt6);
		jp4.add(bt7);
		jp4.add(bt8);
		
		container.setLayout(null);
		container.add(jp1);
		container.add(jp2);
		container.add(jp3);
		container.add(jp4);
		
		jf.setVisible(true);
		jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		jf.setSize(300, 400);
		jf.setLocation(100, 100);
		
		bt1.addActionListener(this);
		bt2.addActionListener(this);
		bt3.addActionListener(this);
		bt4.addActionListener(this);
		bt5.addActionListener(this);
		bt6.addActionListener(this);
		bt7.addActionListener(this);
		bt8.addActionListener(this);
		
		
		}

	
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		DBoper db=new DBoper();
		db.getConnection();
		if(e.getSource()==bt1){
			double money=db.pstmtQuery_balance(user);
			Query qu=new Query(money);
		}
		if(e.getSource()==bt2){
			Save sa=new Save();
			sa.user.setCard_id(user.getCard_id());
			
		}
		if(e.getSource()==bt3){
			Draw dr=new Draw();
			dr.user.setCard_id(user.getCard_id());
		}
		if(e.getSource()==bt4){
		Transfer dr=new Transfer();
			dr.user.setCard_id(user.getCard_id());
		}
		if(e.getSource()==bt5){
			Gaimi gm=new Gaimi();
			gm.user.setCard_id(user.getCard_id());
			
		}
		if(e.getSource()==bt6){
		if( db.pstmtDelete(user)){
			javax.swing.JOptionPane.showMessageDialog(this,"用户已注销!");
			jf.dispose();}
		}
		if(e.getSource()==bt7){
			javax.swing.JOptionPane.showMessageDialog(this,"欢迎下次使用!");
			System.exit(0);
		}
		if(e.getSource()==bt8){
			Bank lg=new Bank();
			jf.dispose();
		}
		
	}
	
	}
 
 class Register extends JFrame implements ActionListener{
	 private JLabel lab1,lab2,lab3,lab4,lab5,lab6;
	 private JTextField jtf1,jtf4;
	 private JPasswordField jtf2,jtf3;
	 private JButton jb1,jb2;
	 private JRadioButton rbt1,rbt2,rbt3,rbt4;
	 private JPanel jp1,jp2,jp3,jp4,jp5,jp6,jp7;
	 Account user =new Account();
	 JFrame jf =new JFrame("ATM");
	
	 Register(){
	
     Container container=jf.getContentPane();
     
     lab1=new JLabel("身份证号: ");
     lab2=new JLabel("密码:");
     lab3=new JLabel("确认密码:");
     lab4=new JLabel("用户姓名:");			 
	 lab5=new JLabel("用户性别： ");
	 lab6=new JLabel("卡类型:");
	 jtf1=new JTextField(12);
	 jtf2=new JPasswordField(12);
	 jtf3=new JPasswordField(12);
	 jtf4=new JTextField(12);
	 jb1=new JButton("确定");
	 jb2=new JButton("返回");
     jp1=new JPanel();	 
     jp2=new JPanel();
     jp3=new JPanel();
     jp4=new JPanel();
     jp5=new JPanel();
     jp6=new JPanel();
     jp7=new JPanel();
	 rbt1=new JRadioButton("男");
	 rbt2=new JRadioButton("女");
	 ButtonGroup group1=new ButtonGroup();
	 group1.add(rbt1);
	 group1.add(rbt2);
	 rbt3=new JRadioButton("储蓄卡");
	 rbt4=new JRadioButton("信用卡");
	 ButtonGroup group2=new ButtonGroup();
	 group2.add(rbt3);
	 group2.add(rbt4);
	 
	 
	 jp1.setBounds(40, 60,300, 40);
	 jp1.setLayout(new FlowLayout(FlowLayout.LEFT  ,40,10));
	 jp2.setBounds(68, 120,300, 40);
	 jp2.setLayout(new FlowLayout(FlowLayout.LEFT ,40,10));
	 jp3.setBounds(40, 180,300, 40);
	 jp3.setLayout(new FlowLayout(FlowLayout.LEFT,40,10));
	 jp4.setBounds(40, 240,300, 40);
     jp4.setLayout(new FlowLayout(FlowLayout.LEFT,40,10));
	 jp5.setBounds(40, 300,300, 40);
	 jp5.setLayout(new FlowLayout(FlowLayout.LEFT,40,10));
	 jp6.setBounds(40, 360,300, 40);
	 jp6.setLayout(new FlowLayout(FlowLayout.LEFT,40,10));
	 jp7.setBounds(40, 420,300, 40);
	 jp7.setLayout(new FlowLayout(FlowLayout.CENTER,40,10));
	
	 
	 jp1.add(lab1);
	 jp1.add(jtf1);
	 jp2.add(lab2);
	 jp2.add(jtf2);
	 jp3.add(lab3);
	 jp3.add(jtf3);
	 jp4.add(lab4);
	 jp4.add(jtf4);
	 jp5.add(lab5);
	 jp5.add(rbt1);
	 jp5.add(rbt2);
	 jp6.add(lab6);
	 jp6.add(rbt3);
	 jp6.add(rbt4);
	 jp7.add(jb1);
	 jp7.add(jb2);
	 
	 container.setLayout(null);
	 container.add(jp1);
	 container.add(jp2);
	 container.add(jp3);
	 container.add(jp4);
	 container.add(jp5);
	 container.add(jp6);
	 container.add(jp7);
	 
	 jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
	 jf.setSize(400, 600);
	 jf.setVisible(true);
	 jf.setLocation(100, 100);
	 
	 jb1.addActionListener(this);
	 jb2.addActionListener(this);
	 
	 }

	
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		DBoper db=new DBoper();
		db.getConnection();
		try{
		if(e.getSource()==jb1){
				if(jtf1.getText().equals("")||jtf2.getText().equals("")||jtf3.getText().equals("")
						||jtf4.getText().equals("")||!(rbt1.isSelected()||rbt2.isSelected())||
						!(rbt3.isSelected()||rbt4.isSelected()))
					javax.swing.JOptionPane.showMessageDialog(this,"请将信息填写完整!");
				else{
				if(!(jtf2.getText().equals(jtf3.getText())))
					javax.swing.JOptionPane.showMessageDialog(this,"密码输入错误!");	
				else
					user.setPassword(jtf2.getText());
				}
				user.setId(jtf1.getText());
				user.setName(jtf4.getText());
				if(rbt1.isSelected())
					user.setSex("男");
				if(rbt2.isSelected())
					user.setSex("女");
				if(rbt3.isSelected()){
					user.setCard_type(0);
				    user.setOverdraw(0);}
				if(rbt4.isSelected()){
					user.setCard_id(1);	
					user.setOverdraw(1000);
					}
				
				if(db.pstmtInsert(user)){
					javax.swing.JOptionPane.showMessageDialog(this,"开户成功!");
					jf.dispose();
					new Bank();
				}
				
			
		}
		}catch(Exception ex){
			javax.swing.JOptionPane.showMessageDialog(this,"请将信息填写完整!");
		}
		if(e.getSource()==jb2){
			jf.dispose();
			new Bank();
		}
			
	}
	 	 
	 
 }
 
 class  Query extends JFrame implements ActionListener{
	 private JButton jbt1,jbt2;
	 private JPanel jp1;
	 private JLabel lab1;
	 JFrame jf=new JFrame();
	 	  Query(double money){
		 jbt1 =new JButton("退卡");
		 jbt2=new JButton("返回");
		 jp1=new JPanel();
		 lab1=new JLabel();
		 
		 Container container=jf.getContentPane();
		 
		 lab1.setText("账户余额:   "+money+"元");
		 lab1.setBounds(100, 80, 300, 50);
		 jp1.setBounds(50, 150, 300, 40);
		 jp1.setLayout(new FlowLayout(FlowLayout.CENTER,40,5));
		 jp1.add(jbt1);
		 jp1.add(jbt2);
		 
		 container.setLayout(null);
		 container.add(jp1);
		 container.add(lab1);
		 
		 jf.setVisible(true);
		 jf.setSize(400,300 );
		 jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		 jf.setLocation(100, 100);
		 
		 jbt1.addActionListener(this);
		 jbt2.addActionListener(this);
		 
	 }


		
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			if(e.getSource()==jbt1){
				javax.swing.JOptionPane.showMessageDialog(this,"欢迎下次使用!");
				System.exit(0);
				
			}
			if(e.getSource()==jbt2){
				jf.dispose();
			}	
		} 
 }
 
 class Save extends JFrame implements ActionListener{
	 JLabel lab1;
	 JTextField jtf1;
	 JButton jbt1,jbt2;
	 JPanel jpn1,jpn2;
	 JFrame jf=new JFrame();
	 Account user=new Account();
	 Save(){
		 lab1=new JLabel("存款金额:");
		 jtf1=new JTextField(12);
		 jbt1=new JButton("确定");
		 jbt2=new JButton("返回");
		 jpn1=new JPanel();
		 jpn2=new JPanel();
		 
		 Container container=jf.getContentPane();
		 
		 jpn1.setBounds(40, 50, 300, 40);
		 jpn1.setLayout(new FlowLayout(FlowLayout.CENTER,40,20));
		 jpn2.setBounds(40, 150, 300, 40);
		 jpn2.setLayout(new FlowLayout(FlowLayout.CENTER,40,10));
		 jpn1.add(lab1);
		 jpn1.add(jtf1);
		 jpn2.add(jbt1);
		 jpn2.add(jbt2);
		 
		 container.setLayout(null);
		 container.add(jpn1);
		 container.add(jpn2);
		 		 
		 jf.setVisible(true);
		 jf.setSize(400,300 );
		 jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		 jf.setLocation(100, 100);
		
		 jbt1.addActionListener(this);
		 jbt2.addActionListener(this);
		 
		  

	 }
	
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		DBoper db=new DBoper();
		db.getConnection();
		if(e.getSource()==jbt1){
			try{
				double money=Double.parseDouble(jtf1.getText());
				if(db.pstmtUpdate_save(user, money)){
				javax.swing.JOptionPane.showMessageDialog(this,"存款成功!"); 
				
				}
				
			}catch(Exception ex){
				javax.swing.JOptionPane.showMessageDialog(this,"存款金额不能为空"); 
			}
			
		}
		if(e.getSource()==jbt2){
			jf.dispose();
		}
		
	}
	 
 }
 
 class  Draw extends JFrame implements ActionListener{
	 JLabel lab1;
	 JTextField jtf1;
	 JButton jbt1,jbt2;
	 JPanel jpn1,jpn2;
	 JFrame jf=new JFrame();
	 Account user=new Account();
	 Draw(){
		 lab1=new JLabel("取款金额:");
		 jtf1=new JTextField(12);
		 jbt1=new JButton("确定");
		 jbt2=new JButton("返回");
		 jpn1=new JPanel();
		 jpn2=new JPanel();
		 
		 Container container=jf.getContentPane();
		 
		 jpn1.setBounds(40, 50, 300, 40);
		 jpn1.setLayout(new FlowLayout(FlowLayout.CENTER,40,20));
		 jpn2.setBounds(40, 150, 300, 40);
		 jpn2.setLayout(new FlowLayout(FlowLayout.CENTER,40,10));
		 jpn1.add(lab1);
		 jpn1.add(jtf1);
		 jpn2.add(jbt1);
		 jpn2.add(jbt2);
		 
		 container.setLayout(null);
		 container.add(jpn1);
		 container.add(jpn2);
		 		 
		 jf.setVisible(true);
		 jf.setSize(400,300 );
		 jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		 jf.setLocation(100, 100);
		 
		 jbt1.addActionListener(this);
		 jbt2.addActionListener(this);
		 		 
		 
	 }
	
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		DBoper db=new DBoper();
		db.getConnection();
		if(e.getSource()==jbt1){
			try{
			double money=Double.parseDouble(jtf1.getText());
			if(db.pstmtUpdate_draw(user, money))
				javax.swing.JOptionPane.showMessageDialog(this,"取款成功!!");
			else
				javax.swing.JOptionPane.showMessageDialog(this,"余额不足!");
			}catch(Exception ex){
				javax.swing.JOptionPane.showMessageDialog(this,"存款金额不能为空!");
				
				
			}
		}
		if(e.getSource()==jbt2){
			jf.dispose();
		}
		
		
	}
	 
	 
 }
 
 class Transfer extends JFrame implements ActionListener{
	 JLabel lab1,lab2;
	 JTextField jtf1,jtf2;
	 JButton jbt1,jbt2;
	 JPanel jpn1,jpn2,jpn3;
	 JFrame jf=new JFrame();
	 Account user=new Account();
	 Account user1=new Account();
	 
	 Transfer(){
		 lab1=new JLabel("转入账号:");
		 lab2=new JLabel("转入金额:");
		 jtf1=new JTextField(12);
		 jtf2=new JTextField(12);
		 jbt1=new JButton("确定");
		 jbt2=new JButton("返回");
		 jpn1=new JPanel();
		 jpn2=new JPanel();
		 jpn3=new JPanel();
		 
		 Container container=jf.getContentPane();
		 
		 jpn1.setBounds(40, 40, 300, 40);
		 jpn1.setLayout(new FlowLayout(FlowLayout.CENTER,40,20));
		 jpn2.setBounds(40, 100, 300, 40);
		 jpn2.setLayout(new FlowLayout(FlowLayout.CENTER,40,20));
		 jpn3.setBounds(40, 200, 300, 40);
		 jpn3.setLayout(new FlowLayout(FlowLayout.CENTER,40,10));
		 
		 jpn1.add(lab1);
		 jpn1.add(jtf1);
		 jpn2.add(lab2);
		 jpn2.add(jtf2);
		 jpn3.add(jbt1);
		 jpn3.add(jbt2);
		 
		 container.setLayout(null);
		 container.add(jpn1);
		 container.add(jpn2);
		 container.add(jpn3);
		 
		 
		 jf.setVisible(true);
		 jf.setSize(400,300 );
		 jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		 jf.setLocation(100, 100);
		 
		 jbt1.addActionListener(this);
		 jbt2.addActionListener(this);
		 
	 }

	
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		DBoper db=new DBoper();
		db.getConnection();
		if(e.getSource()==jbt1){
			try{
				int i=Integer.parseInt(jtf1.getText());
				user1.setCard_id(i);
				double money=Double.parseDouble(jtf2.getText());
				if(db.pstmtUpdate_transfer(user, user1, money))
					javax.swing.JOptionPane.showMessageDialog(this,"转账成功!!");
				else
					javax.swing.JOptionPane.showMessageDialog(this,"余额不足,转账失败!");
				}catch(Exception ex){
					javax.swing.JOptionPane.showMessageDialog(this,"账户和转账金额不能为空!");
										
				}		
		}
		if(e.getSource()==jbt2){
			jf.dispose();
		}		
	}	 
 }
 
 class Gaimi extends JFrame implements ActionListener{
	 JLabel lab1,lab2;
	 JPasswordField jtf1,jtf2;
	 JButton jbt1,jbt2;
	 JPanel jpn1,jpn2,jpn3;
	 JFrame jf=new JFrame();
	 Account user=new Account();
	 
	 Gaimi(){
		 lab1=new JLabel("新密码:");
		 lab2=new JLabel("确认密码:");
		 jtf1=new JPasswordField(12);
		 jtf2=new JPasswordField(12);
		 jbt1=new JButton("确定");
		 jbt2=new JButton("返回");
		 jpn1=new JPanel();
		 jpn2=new JPanel();
		 jpn3=new JPanel();
		 
		 Container container=jf.getContentPane();
		 
		 jpn1.setBounds(40, 40, 300, 40);
		 jpn1.setLayout(new FlowLayout(FlowLayout.CENTER,40,20));
		 jpn2.setBounds(35, 100, 300, 40);
		 jpn2.setLayout(new FlowLayout(FlowLayout.CENTER,40,20));
		 jpn3.setBounds(40, 200, 300, 40);
		 jpn3.setLayout(new FlowLayout(FlowLayout.CENTER,40,10));
		 
		 jpn1.add(lab1);
		 jpn1.add(jtf1);
		 jpn2.add(lab2);
		 jpn2.add(jtf2);
		 jpn3.add(jbt1);
		 jpn3.add(jbt2);
		 
		 container.setLayout(null);
		 container.add(jpn1);
		 container.add(jpn2);
		 container.add(jpn3);
		 
		 
		 jf.setVisible(true);
		 jf.setSize(400,300 );
		 jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		 jf.setLocation(100, 100);
		 
		 jbt1.addActionListener(this);
		 jbt2.addActionListener(this);
		 
		 
	 }
	 

	
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		DBoper db=new DBoper();
		db.getConnection();
		if(e.getSource()==jbt1){
			user.setPassword(jtf1.getText());
		   if(jtf1.getText().equals("")||jtf2.getText().equals(""))
			   javax.swing.JOptionPane.showMessageDialog(this,"密码不能为空!"); 
		   else if(!jtf1.getText().equals(jtf2.getText()))
			   javax.swing.JOptionPane.showMessageDialog(this,"密码输入不正确!"); 
		   else  if(db.pstmtUpdate_gaimi(user)){
			   javax.swing.JOptionPane.showMessageDialog(this,"改密成功!"); 
			   jf.dispose();
		   }
			   
		}
		if(e.getSource()==jbt2){
			jf.dispose();
		}
		
		
	}
	 
	 
 }
 
